using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraTab;
using E4RPDevExpress10;
using E4RPTools;
using Movements.Controls;


namespace Movements
{
    public partial class MainRibbonForm : RibbonForm
    {
        private Controller _controller;

        public Controller Controller
        {
            set
            {
                _controller = value;
                _controller.OwnerWindow = this;
                movementListUserControl.Controller = _controller;
                productListUserControl.Controller = _controller;
                supplierListUserControl.Controller = _controller;
                targetEntityListUserControl.Controller = _controller;
                productSetupXtraUserControl.Controller = _controller;
                userListXtraUserControl.Controller = _controller;
            }
        }

        public MainRibbonForm()
        {
            InitializeComponent();
            Load += MainRibbonFormLoad;
            xtraTabControl.SelectedPageChanged += XtraTabControlSelectedPageChanged;
            newMovementBarButtonItem.ItemClick += NewMovementBarButtonItemItemClick;
            printMovementBarButtonItem.ItemClick += PrintMovementBarButtonItemItemClick;
            //
            newDocumentBarButtonItem.ItemClick += NewDocumentBarButtonItemItemClick;
            deleteDocumentButtonItem.ItemClick += DeleteDocumentButtonItemItemClick;
            //
            exportExcelBarButtonItem.ItemClick += ExportExcelBarButtonItemItemClick;
            showColumnsBarButtonItem.ItemClick += ShowColumnsBarButtonItemItemClick;
            printPreviewGridBarButtonItem.ItemClick += PrintPreviewGridBarButtonItemItemClick;
            printGridBarButtonItem.ItemClick += PrintGridBarButtonItemItemClick;
            // Products Group
            newProductLevelBarButtonItem.ItemClick += NewProductLevelBarButtonItemItemClick;
            deleteProductTreeBarButtonItem.ItemClick += DeleteProductTreeBarButtonItemItemClick;
            // Products Group
            saveLayoutBarButtonItem.ItemClick += SaveLayoutBarButtonItemItemClick;
            loadLayoutBarButtonItem.ItemClick += LoadLayoutBarButtonItemItemClick;
            aboutBarButtonItem.ItemClick += AboutBarButtonItemItemClick;
            exitBarButtonItem.ItemClick += ExitBarButtonItemItemClick;
            Closing += MainRibbonFormClosing;
        }

        #region Private Methods

        private bool IsPrintMovementEnale()
        {
            if(xtraTabControl.SelectedTabPage != movementXtraTabPage)
                return false;
            var control = xtraTabControl.SelectedTabPage.Controls[0];
            var gridView = ((IGridView)control).GridView();
            return gridView.SelectedRowsCount == 1;
        }
        
        private bool IsNewDocumentEnable()
        {
            if(xtraTabControl.SelectedTabPage == targetEquipmentXtraTabPage)
                return true;
            if (xtraTabControl.SelectedTabPage == productXtraTabPage)
                return true;
            if (xtraTabControl.SelectedTabPage == supplierXtraTabPage)
                return true;
            if (xtraTabControl.SelectedTabPage == usersXtraTabPage && _controller.LoggedUserProfile == (int)UserProfile.Administrator)
                return true;
            return false;
        }

        private void AddDocument(XtraTabPage xtraTabPage)
        {
            xtraTabControl.SelectedTabPage = xtraTabPage;
            var control = xtraTabControl.SelectedTabPage.Controls[0];
            ((IList)control).AddDocument();
        }

        private bool CanDeleteDocument()
        {
            if (xtraTabControl.SelectedTabPage != targetEquipmentXtraTabPage)
                return false;
            return _controller.LoggedUserProfile == (int) UserProfile.Administrator;
        }

        private void EnableControls()
        {
            printMovementBarButtonItem.Enabled = IsPrintMovementEnale();
            //
            newDocumentBarButtonItem.Enabled = IsNewDocumentEnable();
            deleteDocumentButtonItem.Enabled = CanDeleteDocument();
            // 
            exportExcelBarButtonItem.Enabled = xtraTabControl.SelectedTabPage != productSetupXtraTabPage;
            printGridBarButtonItem.Enabled = xtraTabControl.SelectedTabPage != productSetupXtraTabPage;
            showColumnsBarButtonItem.Enabled = xtraTabControl.SelectedTabPage != productSetupXtraTabPage;
            printPreviewGridBarButtonItem.Enabled = xtraTabControl.SelectedTabPage != productSetupXtraTabPage;
            saveLayoutBarButtonItem.Enabled = xtraTabControl.SelectedTabPage != productSetupXtraTabPage;
            loadLayoutBarButtonItem.Enabled = xtraTabControl.SelectedTabPage != productSetupXtraTabPage;
            //
            newProductLevelBarButtonItem.Enabled = xtraTabControl.SelectedTabPage == productSetupXtraTabPage;
            deleteProductTreeBarButtonItem.Enabled = xtraTabControl.SelectedTabPage == productSetupXtraTabPage;
        }

        private void SetControlVisibity()
        {
            usersXtraTabPage.PageVisible = _controller.LoggedUserProfile == (int) UserProfile.Administrator;
            productXtraTabPage.PageVisible = _controller.LoggedUserProfile == (int)UserProfile.Administrator;
            supplierXtraTabPage.PageVisible = _controller.LoggedUserProfile == (int)UserProfile.Administrator;
            targetEquipmentXtraTabPage.PageVisible = _controller.LoggedUserProfile == (int)UserProfile.Administrator;
            productSetupXtraTabPage.PageVisible = _controller.LoggedUserProfile == (int)UserProfile.Administrator;            
        }

        #endregion

        void MainRibbonFormLoad(object sender, System.EventArgs e)
        {
            SetControlVisibity();
            EnableControls();
        }

        void XtraTabControlSelectedPageChanged(object sender, TabPageChangedEventArgs e)
        {
            EnableControls();
        }
       
        void NewMovementBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            AddDocument(movementXtraTabPage);
        }

        void PrintMovementBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var movementId = movementListUserControl.SelectedMovementId;
            if(!movementId.HasValue)
            {
                MessageBox.Show(this, "Por favor, seleccione um movimento.", "Movimento", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            _controller.PrintMovement(movementId.Value);
        }

        void NewDocumentBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            AddDocument(xtraTabControl.SelectedTabPage);
        }

        void DeleteDocumentButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var control = xtraTabControl.SelectedTabPage.Controls[0];
            ((IList)control).DeleteDocument();
        }

        void ExportExcelBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var control = xtraTabControl.SelectedTabPage.Controls[0];
            DevExpressHelper.ExportToExcel(((IGridView)control).GridView());
        }

        void PrintGridBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var control = xtraTabControl.SelectedTabPage.Controls[0];
            var gridView = ((IGridView)control).GridView();
            gridView.GridControl.Print();
            DevExpressHelper.Print(gridView);
        }

        void ShowColumnsBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var control = xtraTabControl.SelectedTabPage.Controls[0];
            var gridView = ((IGridView)control).GridView();
            DevExpressHelper.ColumnsCustomization(gridView);
        }

        void PrintPreviewGridBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var control = xtraTabControl.SelectedTabPage.Controls[0];
            var gridView = ((IGridView)control).GridView();
            DevExpressHelper.ShowPrintPreview(gridView);
        }

        void NewProductLevelBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var control = xtraTabControl.SelectedTabPage.Controls[0];
            ((IProductSetup)control).AddGroup();
        }

        void DeleteProductTreeBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var control = xtraTabControl.SelectedTabPage.Controls[0];
            ((IProductSetup)control).Remove();
        }

        void SaveLayoutBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var control = xtraTabControl.SelectedTabPage.Controls[0];
            var gridView = ((IGridView)control).GridView();
            var listName = ((IGridView)control).ListName;
            var layoutName = _controller.SaveLayout(this, listName, DevExpressHelper.SaveLayoutToXml(gridView));
            if (layoutName != "")
                layoutNameBarStaticItem.Caption = layoutName;
        }

        void LoadLayoutBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var control = xtraTabControl.SelectedTabPage.Controls[0];
            var gridView = ((IGridView)control).GridView();
            var listName = ((IGridView)control).ListName;
            var layoutName = _controller.LoadLayout(gridView, listName);
            if (layoutName != "")
                layoutNameBarStaticItem.Caption = layoutName;
        }
        
        static void AboutBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var product = new AboutLabelControl("Movimentos", new Font("Jokerman", 24F, FontStyle.Regular, GraphicsUnit.Point, 0), Color.Chocolate, new Point(127, 38));
            var form = new AboutXtraForm { Version = "1.0.0.1", Product = product };
            form.ShowDialog();
        }

        void ExitBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            _controller.CloseConnections();
            Close();
        }

        void MainRibbonFormClosing(object sender, CancelEventArgs e)
        {
            _controller.CloseConnections();
        }
    }
}